#include <linux/init.h>
#include <linux/module.h>
#include <linux/mod_devicetable.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/of.h>

#include "platform_data_special.h"
#include "stm32mp157xxx.h"

struct stm32mp157a{
	unsigned int major;
	struct resource *resource[5];
	struct class *cls;
	struct device *dev;
	GPIO_TypeDef *gpio;
	unsigned int *rcc;
	struct Platform_data_spex *spex;
};
struct stm32mp157a *drv_led;

int drv_hello_probe(struct platform_device *pdev)
{
	int ret =0;
	printk("---%s---\r\n",__FUNCTION__);
	//实例化一个对象
	drv_led= kzalloc(sizeof(struct stm32mp157a), GFP_KERNEL);
	if(IS_ERR(drv_led))
	{
		printk("kzalloc_err\r\n");
		ret = PTR_ERR(drv_led);
		return ret;
	}
	drv_led->spex = kzalloc(sizeof(struct Platform_data_spex), GFP_KERNEL);
	if(IS_ERR(drv_led->spex))
	{
		printk("spex_kzalloc_err\r\n");
		ret = PTR_ERR(drv_led->spex);
		goto spex_kzalloc_err;
	}
	
	drv_led->resource[0] = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	drv_led->resource[1] = platform_get_resource(pdev, IORESOURCE_MEM, 1);
	printk("GPIO: start=0x%x end=0x%x\r\n",drv_led->resource[0]->start,drv_led->resource[0]->end);
	printk("RCC: start=0x%x end=0x%x\r\n",drv_led->resource[1]->start,drv_led->resource[1]->end);

	of_property_read_u32_index(pdev->dev.of_node, "minum", 0, &drv_led->spex->minum);
	of_property_read_string(pdev->dev.of_node, "dev_name", &drv_led->spex->name);
	printk("minum=%d name=%s\r\n",drv_led->spex->minum,drv_led->spex->name);
	
	return ret;	
	spex_kzalloc_err:
		kfree(drv_led);
	return ret;

}
int drv_hello_remove(struct platform_device *pdev)
{
	int ret =0;
	printk("---%s---\r\n",__FUNCTION__);
	kfree(drv_led);
	return ret;
}

//struct platform_device_id hello_id_table[]={
	//{"drv_hello",0x01},
//};


const struct of_device_id hello_table[]={
	{ .compatible = "stm32mp157a_led0" },
	{ .compatible = "stm32mp157a_led1" },
	{ .compatible = "stm32mp157a_led2" },
};

struct platform_driver drv={
	.probe = drv_hello_probe,
	.remove = drv_hello_remove,
	.driver = {
		.name = "hello",
		.of_match_table = hello_table,
	},
};

static int __init drv_hello_init(void)
{
	int ret =0;
	printk("---%s---\r\n",__FUNCTION__);
	platform_driver_register(&drv);
	return ret;
}
void __exit drv_hello_exit(void)
{
	printk("---%s---\r\n",__FUNCTION__);
	platform_driver_unregister(&drv);
}

module_init(drv_hello_init);
module_exit(drv_hello_exit);
MODULE_LICENSE("GPL");

